package com.huluobo.a2210mdmdemo.views

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2210mdmdemo.MyNavHost
import com.huluobo.a2210mdmdemo.R
import com.huluobo.a2210mdmdemo.demo.Demo06
import com.huluobo.apps.view.AppsPage
import com.huluobo.lib_base.RoutePath

/**
 *  Created by LC on 2025/3/26.
 */

sealed class NavItem(val route: String, val title: String, val img: Int) {
    data object MessagePage : NavItem(RoutePath.MESSAGE, "消息", R.drawable.message)
    data object HomePage : NavItem(RoutePath.HOME, "门户", R.drawable.home)
    data object MinePage : NavItem(RoutePath.MINE, "我的", R.drawable.home)
}

@Composable
fun MainPage() {
    val list = listOf(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)
    val navHostController = rememberNavController()

    //获取当前回退栈中所有页面
    val stackEntryState by navHostController.currentBackStackEntryAsState()
    //拿到所有页面的地址
    val destination = stackEntryState?.destination

    Scaffold(
        bottomBar = {
            //需要判断,如果不是首页的3个页面,就不显示底部导航
            if (destination?.hierarchy?.any {
                    //通过拿到底部导航的地址,对比,若果包含底部导航的地址,就显示底部导航栏,否则就不显示
                    //转化list当中的地址,如果包含底部导航的地址
                    list.map { navItem ->
                        navItem.route
                    }.contains(it.route)
                } == true) {
                NavigationBar {
                    list.forEach {
                        NavigationBarItem(selected = false, onClick = {
                            navHostController.navigate(it.route)
                        }, icon = {
                            //底部导航的图标
                            Image(painter = painterResource(id = it.img), contentDescription = "")
                        }, label = {
                            //底部导航的标签
                            Text(text = it.title)
                        })
                    }
                }
            }
        }
    ) {
        MyNavHost(it = it, navHostController = navHostController)
    }
}